package com.aysafety.eventmodel.corn;

import com.aysafety.eventmodel.common.DateUtils;
import com.aysafety.eventmodel.service.hydraulicSupport.HydraulicSupportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: iotDataPlatform
 * @description: 液压支架
 * @author: ChengQi
 * @create: 2019-02-20 19:53
 **/
@Component
@Order(3)
public class HydraulicSupportTask {

    @Autowired HydraulicSupportService hydraulicSupportService;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * @Description: 频次
     * @Param: []
     * @return: void
     * @Author: ChengQi
     * @Date: 2019/2/20
     */
    //@Scheduled(cron = "0 0/1 * * * ? ")
    public void Frequency() {
        String startTime = DateUtils.getTime();
        logger.info("正在计算液压支架频次currentTime..." + startTime);
        try {
            hydraulicSupportService.countFrequency(startTime);
        } catch (Throwable throwable) {
            logger.info("液压支架的频次定时任务:", throwable);
        }

        logger.info("完成液压支架频次计算...startTime:" + startTime);
    }

    //@Scheduled(cron = "30 50 11 3 * ?")
    public void FrequencyHistory() {
        logger.info("正在计算液压支架...");
        try {
            String s = DateUtils.getTime();
            if (!DateUtils.compareDate(s, "2019-04-14 00:00:00")) {
                Calendar c = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String tempStartTime = "2016-09-01 00:00:00";
                String tempEndTime = "2019-04-02 00:00:00";
                Date beginDate = dateFormat1.parse(tempStartTime);
                Date endDate = dateFormat1.parse(tempEndTime);
                Date date = beginDate;
                while (!date.equals(endDate)) {
                    String currentTime = DateUtils.getTime(date);
                    logger.info("正在计算液压支架历史频次currentTime..." + currentTime);
                    hydraulicSupportService.countFrequency(currentTime);
                    c.setTime(date);
                    c.add(Calendar.MINUTE, 10);
                    date = c.getTime();
                }
            }
        } catch (Throwable throwable) {
            logger.info("液压支架的频次定时任务:", throwable);
        }
        logger.info("完成计算液压支架...");
    }

    //@Scheduled(cron = "30 50 11 3 * ?")
    public void FrequencyHistory1() {
        logger.info("正在计算液压支架...");
        try {
            String s = DateUtils.getTime();
            if (!DateUtils.compareDate(s, "2019-04-14 00:00:00")) {
                Calendar c = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String tempStartTime = "2017-05-01 00:00:00";
                String tempEndTime = "2017-09-30 00:00:00";
                Date beginDate = dateFormat1.parse(tempStartTime);
                Date endDate = dateFormat1.parse(tempEndTime);
                Date date = beginDate;
                while (!date.equals(endDate)) {
                    String currentTime = DateUtils.getTime(date);
                    logger.info("正在计算液压支架历史频次currentTime..." + currentTime);
                    hydraulicSupportService.countFrequency(currentTime);
                    c.setTime(date);
                    c.add(Calendar.MINUTE, 10);
                    date = c.getTime();
                }
            }
        } catch (Throwable throwable) {
            logger.info("液压支架的频次定时任务:", throwable);
        }
        logger.info("完成计算液压支架...");
    }

    //@Scheduled(cron = "30 50 11 3 * ?")
    public void FrequencyHistory2() {
        logger.info("正在计算液压支架...");
        try {
            String s = DateUtils.getTime();
            if (!DateUtils.compareDate(s, "2019-04-14 00:00:00")) {
                Calendar c = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String tempStartTime = "2017-09-30 00:00:00";
                String tempEndTime = "2018-01-01 00:00:00";
                Date beginDate = dateFormat1.parse(tempStartTime);
                Date endDate = dateFormat1.parse(tempEndTime);
                Date date = beginDate;
                while (!date.equals(endDate)) {
                    String currentTime = DateUtils.getTime(date);
                    logger.info("正在计算液压支架历史频次currentTime..." + currentTime);
                    hydraulicSupportService.countFrequency(currentTime);
                    c.setTime(date);
                    c.add(Calendar.MINUTE, 10);
                    date = c.getTime();
                }
            }
        } catch (Throwable throwable) {
            logger.info("液压支架的频次定时任务:", throwable);
        }
        logger.info("完成计算液压支架...");
    }

    //@Scheduled(cron = "0 0/1 * * * ? ")
    public void Flicker() {
        String startTime = DateUtils.getTime();
        logger.info("正在计算液压支架闪烁...");
        try {
            hydraulicSupportService.countFlicker(startTime);
        } catch (Throwable throwable) {
            logger.info("液压支架的闪烁定时任务:", throwable);
        }

        logger.info("完成液压支架闪烁计算...startTime:" + startTime);
    }

    //@Scheduled(cron = "30 50 11 3 * ?")
    public void FlickerHistory() {
        try {
            String s = DateUtils.getTime();
            if (!DateUtils.compareDate(s, "2019-04-14 00:00:00")) {
                Calendar c = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String tempStartTime = "2016-09-01 00:00:00";
                String tempEndTime = "2019-04-02 00:00:00";
                Date beginDate = dateFormat1.parse(tempStartTime);
                Date endDate = dateFormat1.parse(tempEndTime);
                Date date = beginDate;
                while (!date.equals(endDate)) {
                    String currentTime = DateUtils.getTime(date);
                    logger.info("正在计算液压支架历史闪烁currentTime..." + currentTime);
                    hydraulicSupportService.countFlicker(currentTime);
                    c.setTime(date);
                    c.add(Calendar.MINUTE, 10);
                    date = c.getTime();
                }
            }
        } catch (Throwable throwable) {
            logger.info("液压支架的频次定时任务:", throwable);
        }
    }

    //@Scheduled(cron = "30 50 11 3 * ?")
    public void FlickerHistory1() {
        try {
            String s = DateUtils.getTime();
            if (!DateUtils.compareDate(s, "2019-04-14 00:00:00")) {
                Calendar c = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String tempStartTime = "2017-05-01 00:00:00";
                String tempEndTime = "2017-09-30 00:00:00";
                Date beginDate = dateFormat1.parse(tempStartTime);
                Date endDate = dateFormat1.parse(tempEndTime);
                Date date = beginDate;
                while (!date.equals(endDate)) {
                    String currentTime = DateUtils.getTime(date);
                    logger.info("正在计算液压支架历史闪烁currentTime..." + currentTime);
                    hydraulicSupportService.countFlicker(currentTime);
                    c.setTime(date);
                    c.add(Calendar.MINUTE, 10);
                    date = c.getTime();
                }
            }
        } catch (Throwable throwable) {
            logger.info("液压支架的频次定时任务:", throwable);
        }
    }

    //@Scheduled(cron = "30 50 11 3 * ?")
    public void FlickerHistory2() {
        try {
            String s = DateUtils.getTime();
            if (!DateUtils.compareDate(s, "2019-04-14 00:00:00")) {
                Calendar c = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String tempStartTime = "2017-09-30 00:00:00";
                String tempEndTime = "2018-01-01 00:00:00";
                Date beginDate = dateFormat1.parse(tempStartTime);
                Date endDate = dateFormat1.parse(tempEndTime);
                Date date = beginDate;
                while (!date.equals(endDate)) {
                    String currentTime = DateUtils.getTime(date);
                    logger.info("正在计算液压支架历史闪烁currentTime..." + currentTime);
                    hydraulicSupportService.countFlicker(currentTime);
                    c.setTime(date);
                    c.add(Calendar.MINUTE, 10);
                    date = c.getTime();
                }
            }
        } catch (Throwable throwable) {
            logger.info("液压支架的频次定时任务:", throwable);
        }
    }


    /**
     * 定时计算闪烁间隔危险度
     */
    //@Scheduled(cron = "0 23 10 * * ?")
    public void countFlickerIntervalRiskHistory() {
        try {
            String s = DateUtils.getTime();
            if (!DateUtils.compareDate(s, "2019-04-19 00:00:00")) {
                Calendar c = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String tempStartTime = "2017-05-01 00:00:00";
                String tempEndTime = "2019-04-14 00:00:00";
                Date beginDate = dateFormat1.parse(tempStartTime);
                Date endDate = dateFormat1.parse(tempEndTime);
                Date date = beginDate;
                Map<String, Map<String, Long>> lastStrMap = new HashMap<>();
                while (!date.equals(endDate)) {
                    String currentTime = DateUtils.getTime(date);
                    logger.info("正在计算液压支架闪烁间隔危险度currentTime..." + currentTime);

                    lastStrMap = hydraulicSupportService.flickerIntervalRisk(lastStrMap, currentTime);
                    c.setTime(date);
                    c.add(Calendar.MINUTE, 60);
                    date = c.getTime();
                }
                logger.info("历史闪烁危险度计算完成...");
            }
        } catch (Throwable e) {
            logger.error("闪烁间隔危险度: ", e);
        }
    }

    //@Scheduled(cron = "0 17 11 * * ?")
    public void countFlickerIntervalRisk() {
        String startTime = DateUtils.getTime();
        logger.info("正在计算液压支架闪烁危险度...");
        try {
            hydraulicSupportService.flickerIntervalRiskRealTime(startTime);
        } catch (Throwable throwable) {
            logger.info("液压支架的闪烁危险度定时任务:", throwable);
        }

        logger.info("完成液压支架闪烁危险度计算...startTime:" + startTime);
    }


    //@Scheduled(cron = "0 59 15 * * ?")
    public void warningRealtime() {
       // String startTime = DateUtils.getTime();
        String currentTime = DateUtils.getTime();
        logger.info("正在计算液压支架综合预警WcurrentTime..." + currentTime);
        try {
            hydraulicSupportService.warning(currentTime);
        } catch (Throwable throwable) {
            logger.error("frequency error,currentTime:" + currentTime);
        }
        logger.info("完成液压支架综合预警W计算currentTime..." + currentTime);
    }

    //@Scheduled(cron = "30 31 16 5 * ? ")
    public void warningRealtimeHistory() {
        logger.info("正在计算综合预警W...历史");
        try {
            String s = DateUtils.getTime();
            if (!DateUtils.compareDate(s, "2019-05-07 13:00:00")) {
                Calendar c = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // 开始时间必须小于结束时间
                Date beginDate = dateFormat1.parse("2018-01-01 00:00:00");
                Date endDate = dateFormat1.parse("2019-04-16 00:00:00");
                Date date = beginDate;
                while (!date.equals(endDate)) {
                    String currentTime = DateUtils.getTime(date);
                    hydraulicSupportService.warning(currentTime);
                    c.setTime(date);
                    c.add(Calendar.MINUTE, 60);
                    date = c.getTime();
                    logger.info("程序运行中..." + date.toString());
                }
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }


}
